var imgArray = []
var seatType = "0"
var shopId =ue.getStorage("shopId")
if (!shopId){
    window.location.href = "../index.html?infrom=1"
}

$(function () {

    $('#advTitle').hide();
    $('.model').hide();
    $("[data-toggle='date']").calendar({
        inputReadOnly: true,
        dateFormat: 'yyyy-mm-dd',
        minDate: Date.now() - 24 * 3600000,
        dayNamesShort: "日一二三四五六".split(""),
        onClose: function () {
            var startTime = $('input[name="startTime"]').val()
            var endTime = $('input[name="endTime"]').val()
            startTime && endTime && selectSeat(seatType)
        }
    });
    $("#seatType").change(function () {
        seatType = $('#seatType').val()
        $('input[name="startTime"]').val("")
        $('input[name="endTime"]').val("")
        $('#bank option').remove()
        $('#bank').append(' <option value="">选择发布位置</option>')
        if (seatType == "0") {
            $('input[name="advTitle"]').val("")
            $('#advTitle').hide();
        } else {
            $('#advTitle').show();
        }
    })

    //图片的上传方法
    var tmpl = '<li class="weui-uploader__file" style="background-image:url(#url#);position: relative;">' +
        '<span class="del" style="width:20px;height:20px;position: absolute;top:0px;right:-5px;border-radius: 50%;' +
        'display: block;background:url(../images/cha.png)no-repeat center;background-size:100% 100%;"></span></li>',
        $uploaderInput = $("#uploaderInput"),
        $uploaderFiles = $("#uploaderFiles");

    $uploaderInput.on("change", function (e) {
        //填充图片数组
        for (var nu = 0; nu < $("#uploaderInput")[0].files.length; nu++) {
            imgArray[0] = ($("#uploaderInput")[0].files[nu])
        }
        var src, url = window.URL || window.webkitURL || window.mozURL,
            files = e.target.files;
        for (var i = 0, len = files.length; i < len; ++i) {
            var file = files[i];
            if (url) {
                src = url.createObjectURL(file);
            } else {
                src = e.target.result;
            }

            $uploaderFiles.empty().append($(tmpl.replace('#url#', src)));
        }
        $('#imgNum').text('1/1')
    });
    //图片删除
    $(document).on('click', '.weui-uploader__file .del', function (event) {
        event.stopPropagation();
        imgArray.remove($(this).parents('li').index())
        $(this).parents('li').remove();
        $('#imgNum').text('0/1')
    })


    $(".mt10").on("click", function () {
        var _this = $(this)
        if (!$("#weuiAgree").is(':checked')) {
            $.toast("请勾选同意相关条款", "forbidden");
            return false;
        }
        var formData = new FormData();
        if (imgArray.length == 0) {
            //无营业执照
            $.toast("请上传图片", "forbidden");
            return false;
        } else {
            for (var i = 0; i < imgArray.length; i++) {
                formData.append("imgs", imgArray[i])
            }
        }

        if ($('input[name="endTime"]').val()) {
            formData.append("endTime", $('input[name="endTime"]').val())
        } else {
            return false;
            $.toast("结束时间不能为空", "forbidden");
        }
        if ($('input[name="startTime"]').val()) {

            formData.append("startTime", $('input[name="startTime"]').val())
        } else {
            return false;
            $.toast("开始时间不能为空", "forbidden");
        }
        if ($("#bank option:selected").val()) {
            formData.append("seatId", $("#bank option:selected").val())
        } else {
            return false;
            $.toast("选择位置不能为空", "forbidden");
        }
        formData.append("shopId", shopId)
        if (seatType == "1") {
            if ($('input[name="advTitle"]').val()) {
                formData.append("advTitle", $('input[name="advTitle"]').val())
            } else {
                return false;
                $.toast("标题不能为空", "forbidden");
            }
            formData.append("text2", $('#text2').val())
            _this.addClass("weui-btn_disabled")
            $.ajax({
                url: getWeiContextPath() + "/wx/adv/appplySeat",
                type: 'post',
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                beforeSend:function(){
                    $.showLoading("数据上传中")
                },
                success: function (res) {
                    $.hideLoading()
                    if (res.code == 0) {
                        // 申请成功
                        $.toast(res.msg, function () {
                            //返回上一页既用户中心
                            window.history.go(-1)
                        });
                    } else {
                        _this.removeClass("weui-btn_disabled")
                        $.toast("申请失败", "forbidden");
                    }
                }
            });
        } else {
            formData.append("comments", $('#text2').val())
            _this.addClass("weui-btn_disabled")
            $.ajax({
                url: getWeiContextPath() + "/wx/data/appplySeat",
                type: 'post',
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                beforeSend:function(){
                    $.showLoading("数据上传中")
                },
                success: function (res) {
                    $.hideLoading()
                    if (res.code == 0) {
                        // 申请成功
                        $.toast(res.msg, function () {
                            //返回上一页既用户中心
                            window.history.go(-1)
                        });
                    } else {
                        _this.removeClass("weui-btn_disabled")
                        $.toast("申请失败", "forbidden");
                    }
                }
            });

        }
    })

    /*弹框确认按钮*/
    $('#confirm').on('click',function(){
        $('#weuiAgree').attr("checked", true);
        $('.model').hide()
    });

    //弹框取消按钮
    $('#quit').on('click',function(){
        $('.model').hide()
    });

})

/*查询可用位置*/
function selectSeat(seatType) {
    var startTime = $('input[name="startTime"]').val()
    var endTime = $('input[name="endTime"]').val()
    if (!startTime || !endTime) {
        $.toast("时间未填写完整", "forbidden")
        return false;
    }
    if (endTime < startTime) {
        $.toast("结束时间必须大于开始时间", "forbidden")
        return false;
    }
    if (seatType == "0") {    //轮播
        $.get(getWeiContextPath() + "/wx/data/selectSeatList", {
            startTime: startTime,
            endTime: endTime
        }, function (res) {
            var _bank = $('#bank').empty()
            $.each(res, function (i, obj) {
                _bank.append('<option value="' + obj.id + '">' + obj.seatName + '</option>')
            })
        })

    } else {     //列表
        $.get(getWeiContextPath() + "/wx/adv/selectSeatList", {startTime: startTime, endTime: endTime}, function (res) {
            var _bank = $('#bank').empty()

            $.each(res, function (i, obj) {
                _bank.append('<option value="' + obj.id + '">' + obj.seatName + '</option>')
            })
        })
    }
}

/*查看协议*/
function seeXieYi() {
    $(".model iframe").prop('src', 'xieyi.html')
    $('.model').show()
}



